package cn.foolishbird.hamal.frame.base;


import cn.foolishbird.hamal.frame.bsassert.info.RstInfo;
import cn.foolishbird.hamal.frame.bsassert.info.SysInfo;
import cn.foolishbird.hamal.frame.exception.base.BaseException;

/**
 * 返回处理工厂
 *
 * @author Eric
 * @version 1.0
 * @date 2019-08-18
 */
public class RstFactory {

    public static Rst success() {
        return convertor(SysInfo.SUCCESS, new Object());
    }

    public static <T> Rst<T> success(T t) {
        return convertor(SysInfo.SUCCESS, t);
    }

    public static Rst fail() {
        return convertor(SysInfo.RUN_TIME, new Object());
    }

    public static <T> Rst<T> fail(T t) {
        return convertor(SysInfo.RUN_TIME, t);
    }

    public static <T> Rst<T> custom(RstInfo rEnum, T t) {
        return convertor(rEnum, t);
    }

    public static <T> Rst<T> custom(BaseException ex, T t) {
        return new Rst<T>(ex.getCode(), ex.getContent(), t);
    }

    private static <T> Rst<T> convertor(RstInfo info, T t) {
        return new Rst<T>(info.code(), info.content(), t);
    }
}
